- [Instructor] In the previous movie
I showed you how to create a plot
in the form of a line graph.
In this movie I will show you how to create
several other plot types.
Bar charts also called column charts,
bar h charts which are horizontal bar charts,
and histograms.
I will also show you how to display multiple plots
in the figure area.
I've started MATLAB and I need to create a script.
So I will go to the home tab and click new script.
I could have also pressed Control + N.
The first command I'll put in is clf
and what that does is it clears the figure area.
So if you had any figures, any graphics,
any plots before this they would be cleared
and press Enter.
The scenario I have in mind is to summarize
store sale data.
So I'll start by establishing my store IDs.
So I'll type store ID
equal and there will be numbers incrementing by one
in the range 147 to 150.
So 147:150
and I will suppress the output of that variable
with a semicolon.
Next are the sales so I'll type sales equal
and this will be a vector of 241
space 503, space 541, space 398
right square bracket and
semicolon, there we go.
The command to create a bar chart is simply bar, B-A-R
left parenthesis and the
x values will be store ID.
Those are the categories then a comma and sales
for the y or vertical axis.
Okay, I have all of that there, press Enter.
I can also specify the title for the entire chart
as well as the labels for the x and y axis.
So I'll make the title and parenthesis and single quote
because it's a string, sales by store,
single quote, Enter, and then x label.
Again we're doing string is store ID
and y label is sales and thousands.
We'll just do three zeros followed by an s.
Close up the string as I did all of the others
with a single quote, close the argument label
or argument list with a right parenthesis then Enter
and there we go.
Now I can save my file.
So I'll press Control + s
and call it other plots
and press Enter to save.
Now that my file is ready to go I can close it
and in the command window
I will run it by typing other plots and Enter.
There is see my figure.
It looks good, I have the store IDs that I expected
and they're associated with the proper sales.
The first time you run MATLAB and create figures
it might take a few extra seconds for
the program to create it.
It needs to install the user interface elements
that are required for figures
and it doesn't do that until it has to.
Okay, I will go ahead and close out the figure one window
and I'll make some changes to my code over in the
other plots file, so I will double click it to open it.
Great.
I want to create a second plot
that will be a horizontal bar chart
and then later I want to display it and the original chart
and one other in the same figure area.
So I'm going to start by
copying the code that I have here.
I won't copy clf yet.
Instead I will copy my store ID, sales,
all the other values.
So I've selected them, Control + c to copy
and then I'll give myself a blank line
to start on line nine and press
Control + v to paste.
All right, good.
Now I'm going to comment out the code on lines
two through seven and to do that I'll just add
a percent sign which indicates comment.
Anytime you want to exclude a particular line from running,
for example if you print out the value of a variable to test
what it is at a particular point in a procedure
you can just comment it out
when you no longer need it
and then delete it at some point in the future.
Okay, I have my store ID and sales data, which I will use
and actually I didn't need to copy that
I could have just left the definitions up at top.
Well I'll leave it the way it is for now.
So I want to make this a bar h chart,
so horizontal bar chart.
Now what that means is that the categories will appear
on the vertical or y axis
and the sales values will appear on the horizontal,
but you don't have to switch them
within the bar h function,
because the function knows, it knows store ID is category
and sales are the values and it knows how to display them.
What it doesn't do however is
switch the x label and y label automatically.
This one we need to do that by hand.
So I'll make the x label the y label
and y label the x label.
Okay, everything looks good, press Control + s.
Everything that I wanted to have commented out
is commented out.
So I will close the editor and press the up arrow key
to display the most recent command list.
Other plots appears, which was the name of the script
I want to run, press Enter
and there it appears.
See I have sales by stores, store ID,
and sales in thousands as expected.
I'll go ahead and close that out.
The final chart type that
I would like to work with is a histogram.
A histogram displays the count
of values that occur within certain ranges.
So for example if you have 31 days of sales
then you could count the number of days that have sales
between 10 and 20, 20 and 30 thousand and so on.
So let's see how to create one of those.
I will double click other plots to start editing
and I'm going to use an entirely new set of data here.
So I will comment out
the values for the first two charts,
but I am going to use them later.
So that's why I'm not erasing them.
All right, we're good there, press Enter
and I'll give myself one more line, okay.
The first thing you need to do for the histogram
is to find the data.
So I will do sales two as my new variable
equals and I will do 31 random integers from one to 200.
So random integers are generated using rand i
followed by left parenthesis
and then I want values from one to 200
so the maximum value is 200 comma
and then the number of rows will be one
and the number of values will be 31.
Type your right parenthesis to close out
and a semicolon to suppress the output and Enter.
Now I can create a histogram
using that vector, so that'll just be histogram,
sales two, Enter.
I can also set the x label and the y label.
So I'll do x label parenthesis and single quote sales,
close it out, y label will be count.
That's what the histogram does, it counts,
and then the title in parenthesis
and single quotes would be sales by amount.
Okay, everything looks good.
Press Enter, press Control + s to save
and I will click to close the window
and press v + up arrow key to highlight other plots,
the name of my script, Enter and there I get it.
Now your values might appear different,
but what I see here are
eight sales from zero to 40
or zero to 50
and then seven days out of 31 with sales from 50 to 100
and then eight again for 100 to 150 and 150 to 200.
Because these are random values your chart
or graph or figure might look different.
However, this appears to be a fairly
standard distribution.
Okay, everything looks good,
so I will go ahead and close out of figure number one
and now I can show you how to put
multiple plots in the same figure area.
So I will go to other plots,
double clicking it to edit
and now I can add the sub plot command.
What that does is it identifies a particular graph
as a sub plot
within a larger figure area.
So I'm going to
above store ID press Enter
and to give myself a blank line.
So that will be sub plot left parenthesis
and then I want to have three positions horizontally
and then a comma one vertically comma
and then I'll identify the place
of this first sub plot with a one.
So I've just identified the place for the first plot here
and now I can remove
all of the comment markers.
Okay, so now I can do sub plot three,
one, two.
So this will go on the second position
and again I can remove the comment markers.
Okay, and then the last one would be sub plot
three, one, three.
All right, now I can press Control + s to save
and if I did everything correctly
after I close this, and I see green here
so no warnings are found,
I can press the up arrow key
press Enter for other plots
and there I get my three.
You can see that I have three rows,
one column and the charts are in the positions
that I expected.
So as you can see you have a lot of control
over your plots and charts in MATLAB.